package com.limh.readers.fragment

import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.BindView
import butterknife.OnClick
import com.limh.readers.R
import com.limh.readers.activity.LoginActivity
import com.limh.readers.activity.SettingActivity
import com.limh.readers.activity.UserInfoActivity
import com.limh.readers.bean.User
import com.limh.readers.utils.GsonUtils
import com.limh.readers.utils.LogUtil
import com.limh.readers.utils.SharePreferUtils
import cn.sharesdk.onekeyshare.OnekeyShare


/**
 * Function:期刊
 * author: limh
 * time:2017/7/3
 */
class MineFragment : BaseFragment() {
    @BindView(R.id.txt_fav)
    lateinit var txtFav: TextView
    @BindView(R.id.txt_user)
    lateinit var txtUserName: TextView

    private var user: User? = null

    private val TAG: String = "MineFragment"
    override fun findViews(inflater: LayoutInflater?, container: ViewGroup?): View {
        return inflater?.inflate(R.layout.fragment_mine, container, false) as View
    }

    override fun initViews() {

    }

    override fun initDatas() {
    }

    override fun onResume() {
        super.onResume()
        if ("" != SharePreferUtils.get(activity, "user", "")) {
            user = GsonUtils.str2Bean(SharePreferUtils.get(activity, "user", "") as String, User::class.java)
            txtUserName.text = user?.username
        } else {
            user = null
            txtUserName.text = "登陆/注册"
        }
    }

    @OnClick(R.id.line_setting, R.id.line_opinion, R.id.line_header, R.id.line_share)
    fun OnClick(view: View) {
        when (view.id) {
            R.id.line_setting -> openActivity(SettingActivity::class.java)
            R.id.line_header -> {
                //已经登陆则打开用户详情页面  否则打开登陆页面
                LogUtil.w(TAG, "user=" + user.toString())
                if (null == user) {
                    openActivity(LoginActivity::class.java)
                } else {
                    val intent: Intent = Intent(context, UserInfoActivity::class.java)
                    intent.putExtra("user", SharePreferUtils.get(activity, "user", "") as String)
                    startActivity(intent)
                }
            }
            R.id.line_opinion -> openMarket()
            R.id.line_share -> showShare()
        }
    }

    /**
     * 打开应用市场app详情页面
     */
    private fun openMarket() {
        val uri = Uri.parse("market://details?id=com.limh.readers")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        activity.startActivity(intent)
    }

    private fun showShare() {
        val oks = OnekeyShare()
        //关闭sso授权
        oks.disableSSOWhenAuthorize()

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.app_name))
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("https://fir.im/ga36")
        // text是分享文本，所有平台都需要这个字段
        oks.text = "三五分钟不知道干什么，打开[读·享]，悦读、享受，打发时间。等餐时、坐公交时、蹲马桶时、睡觉前、上班偷个懒等等这些闲暇时间【读·享】陪伴在你的身边！！！"
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("http://firicon.fir.im/bdc30f5cbadcc5bf8bda30c4e71747dfc9defdc6")//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("https://fir.im/ga36")
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("【读·享】一刻")
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name))
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("https://fir.im/ga36")

        // 启动分享GUI
        oks.show(context)
    }

    fun showMsgDialog() {

    }
}